#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;

struct ReturnObject
{
    struct promise_type
    {
        uint32_t value_;
        ReturnObject get_return_object()
        {
            return {
                .h_ = std::coroutine_handle<promise_type>::from_promise(*this)};
        }
        std::suspend_never initial_suspend()
        {
            return {};
        }
        std::suspend_never final_suspend() noexcept
        {
            return {};
        }
        void unhandled_exception()
        {
        }
    };
    std::coroutine_handle<promise_type> h_;
    operator std::coroutine_handle<promise_type>() const
    {
        return h_;
    }
};

template <typename PromisType>
struct GetPromise
{
    PromisType *p_;
    bool await_ready()
    {
        return false;
    }
    bool await_suspend(std::coroutine_handle<PromisType> h)
    {
        p_ = &h.promise();
        return false;
    }
    PromisType *await_resume()
    {
        return p_;
    }
};

[[nodiscard]] ReturnObject counter()
{
    auto pp = co_await GetPromise<ReturnObject::promise_type>{};
    for (size_t i = 0;; ++i)
    {
        pp->value_ = i;
        co_await std::suspend_always{};
    }
}

void test1()
{
    std::coroutine_handle<ReturnObject::promise_type> h = counter();
    ReturnObject::promise_type &promise = h.promise();
    for (int i = 0; i < 3; ++i)
    {
        std::cout << "In get value from promise = " << promise.value_
                  << std::endl;
        h();
    }
    h.destroy();
}

int main(int argc, char **argv)
{
    test1();
    return 0;
}
